using UnityEngine;
using QFramework;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace Survivor
{
    public partial class Enemy : ViewController
    {
        public float MovementSpeed = 5f;
        public int Hp = 3;

        private void Start()
        {
            EnemyGenerator.EnemyCnt.Value++;
        }


        private void FixedUpdate()
        {
            if (Player.Instance)
            {
                var dir = (Player.Instance.transform.position - transform.position).normalized;
                SelfRigidbody2D.velocity = dir * MovementSpeed;
            }
            else
            {
                SelfRigidbody2D.velocity = Vector2.zero;
            }
        }

        private void Update()
        {
            if (Hp <= 0)
            {
                Global.GeneraterPowerUp(gameObject.transform.position);
                gameObject.DestroySelfGracefully();
            }
        }

        private void OnDestroy()
        {
            EnemyGenerator.EnemyCnt.Value--;
        }

        private bool isIgnoreHurt;
        public void Hurt(int value)
        {
            if (isIgnoreHurt) return;
            isIgnoreHurt = true;
            Spite.color = Color.red;
            AudioKit.PlaySound("Hit");
            FloatingTextController.PlayFloatingText(transform.position, value.ToString());
            ActionKit.Delay(0.1f, () =>
            {
                Hp -= value;
                Spite.color = Color.white;
                isIgnoreHurt = false;
            }).Start(this);
        }
    }
}